package com.jwthhealth_pub.old.subscribe.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.Address.Address;
import com.jwthhealth_pub.old.common.Address.AddressDao;
import com.jwthhealth_pub.old.common.Constant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择城市
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeCityActivity extends BaseActivity {
    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.city_list)
    RecyclerView cityList;

    private AddressDao mAddressDao = AddressDao.getInstance();
    private String provinceName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_city);

        ButterKnife.bind(this);

        Intent intent = getIntent();
        String spid = intent.getStringExtra(Constant.PROVINCEID);
        provinceName = intent.getStringExtra(Constant.PROVINCENAME);
        List<Address> citys = mAddressDao.getCitys(spid);
        cityList.setLayoutManager(new LinearLayoutManager(this));
        cityList.setAdapter(new ProvinceAdapter(citys));

    }

    @Override
    protected void title() {
        titleLayout.setTitle(getResources().getString(R.string.subscribe_history_address_city));
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
           @Override
           public void leftclick() {
               finish();
           }

           @Override
           public void leftTextClick() {

           }

           @Override
           public void rightclick() {

           }

           @Override
           public void rightTextClick() {

           }
       }
        );
    }

    private class ProvinceAdapter extends RecyclerView.Adapter {
        private List<Address> cities;

        private ProvinceAdapter(List<Address> cities) {
            this.cities = cities;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(View.inflate(SubscribeCityActivity.this, R.layout.item_address_subscribe, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final ViewHolder vh = (ViewHolder) holder;
            final int pos = vh.getAdapterPosition();
            String cityName = cities.get(position).getName();
            if (cityName.equals("市辖区") || cityName.equals("省直辖县级行政区划") || cityName.equals("县")){
                vh.name.setText(provinceName);
            } else {
                vh.name.setText(cities.get(position).getName());
            }

            vh.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(SubscribeCityActivity.this, SubscribeZoneActivity.class);
                    Address address = cities.get(pos);
                    intent.putExtra(Constant.CITYID, address.getCid());
                    intent.putExtra(Constant.PROVINCENAME, provinceName);
                    intent.putExtra(Constant.CITYNAME, vh.name.getText().toString().trim());
                    if (cities.size() > 1) {
                        intent.putExtra(Constant.CITYTYPE, 1);
                    } else {
                        intent.putExtra(Constant.CITYTYPE, 0);
                    }
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return cities.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            public TextView name;

            public ViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.text);
            }
        }
    }
}
